Page({
  data: {
    password: '',
    confirmPassword: '',
    showPassword: false,
    showConfirmPassword: false
  },

  // 密码输入
  onPasswordInput(e) {
    this.setData({
      password: e.detail.value
    });
  },

  // 确认密码输入
  onConfirmPasswordInput(e) {
    this.setData({
      confirmPassword: e.detail.value
    });
  },

  // 切换密码显示
  togglePassword() {
    this.setData({
      showPassword: !this.data.showPassword
    });
  },

  // 切换确认密码显示
  toggleConfirmPassword() {
    this.setData({
      showConfirmPassword: !this.data.showConfirmPassword
    });
  },

  // 设置密码
  setPassword() {
    const { password, confirmPassword } = this.data;
    
    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      });
      return;
    }
    
    if (password.length < 6) {
      wx.showToast({
        title: '密码长度不能少于6位',
        icon: 'none'
      });
      return;
    }
    
    if (!confirmPassword) {
      wx.showToast({
        title: '请确认密码',
        icon: 'none'
      });
      return;
    }
    
    if (password !== confirmPassword) {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '设置中...'
    });

    // 模拟设置密码
    setTimeout(() => {
      wx.hideLoading();
      
      // 更新本地存储的用户信息
      const userInfo = wx.getStorageSync('userInfo') || {};
      userInfo.hasPassword = true;
      userInfo.password = password; // 实际项目中密码应该加密存储
      wx.setStorageSync('userInfo', userInfo);
      
      wx.showToast({
        title: '设置成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            // 返回个人中心
            wx.navigateBack({
              delta: 2 // 返回两层，跳过绑定手机号页面
            });
          }, 2000);
        }
      });
    }, 1000);
  }
}); 